package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.TemplateCreateRequest;
import com.jmp.base.dto.TemplateCreateResponse;
import com.jmp.base.dto.TemplateGetRequest;
import com.jmp.base.dto.TemplateGetResponse;
import com.jmp.feign.CrmFegin;
import com.jmp.feign.TemplateFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.jmp.api.dto.TemplateDiyRequest;
import com.jmp.api.dto.TemplateDiyResponse;
import com.jmp.api.dto.TemplateListRequest;
import com.jmp.api.dto.TemplateListResponse;
import one.stand.dto.ConvertUtils;
import one.stand.mapper.TemplateApiMapper;
import one.stand.model.ResultModel;
import one.stand.model.Template;
import one.stand.request.BusinessCardRequest;
import one.stand.service.sys.UserContentService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Api("模板管理")
@RestController
@RequestMapping("crmApi/template")
public class TemplateController {
    @Autowired
    private CrmFegin crmFegin;
    @Autowired
    private TemplateFeign templateFeign;

    @Autowired
    private TemplateApiMapper templateApiMapper;

    @ApiOperation("节日海报列表")
    @PostMapping("list")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<TemplateListResponse> list(@RequestAttribute TemplateListRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        final Integer companyId = optUser.getCompanyId();

        PageHelper.startPage(1, 10);
        List<Template> templates = templateApiMapper.selectList(UserContentService.TYPE_COMPANY, companyId, UserContentService.TYPE_CARD);

        List<TemplateListResponse.Template> templateList = ConvertUtils.list(templates, template -> {
            TemplateGetRequest getRequest = TemplateGetRequest.builder()
                    .templateId(template.getId())
                    .cardId(request.getMyCardId())
                    .build();
            ResultModel<TemplateGetResponse> getResponseResultModel = templateFeign.get(getRequest);
            String imgUrl = getResponseResultModel.isSuccess() ? getResponseResultModel.getData().getImageUrl() : null;
            return TemplateListResponse.Template.builder()
                    .id(template.getId())
                    .imgUrl(imgUrl)
                    .imgWidth(template.getImgWidth())
                    .imgHeight(template.getImgHeight())
                    .build();
        });

        //名片海报
        BusinessCardRequest cardRequest = new BusinessCardRequest();
        cardRequest.setCardId(request.getMyCardId());
        cardRequest.setId(Long.valueOf(request.getId()));
        cardRequest.setId(Long.valueOf(request.getId()));
        ResultModel<List<String>> card3 = crmFegin.getCardPoster(cardRequest);
        if (card3.isSuccess()) {
            List<TemplateListResponse.Template> card3List = ConvertUtils.list(card3.getData(), String -> TemplateListResponse.Template.builder()
                    .imgUrl(String).build());
            templateList.addAll(card3List);
        }

        TemplateListResponse response = TemplateListResponse.builder()
                .templates(templateList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("保存自定义")
    @PostMapping("diy")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<TemplateDiyResponse> saveDiy(MultipartFile file, @RequestAttribute TemplateDiyRequest request) throws IOException {
        TemplateCreateRequest createRequest = TemplateCreateRequest.builder()
                .templateId(request.getTemplateId())
                .cardId(request.getMyCardId())
                .diyImg(file.getInputStream())
                .build();
        ResultModel<TemplateCreateResponse> createResponseResultModel = templateFeign.create(createRequest);
        AssertUtil.isTrue(createResponseResultModel);
        String cardUrl = createResponseResultModel.getData().getImageUrl();

        TemplateDiyResponse response = TemplateDiyResponse.builder().cardUrl(cardUrl).build();

        return ResultModel.success(response);
    }
}
